dp greedy implementation *2200

Please click on ads to support us..

Python Code:



import io, os, sys, math, bisect, collections, string


input = lambda : sys.stdin.readline().strip()


def solve():
    (n,m) = map(int, input().split())
    a = list(map(int, input().split()))
    trainings_by_tasks = [[] for _ in range(n)]
    for i in range(m):
        (e,t,p) = map(int, input().split())
        e -= 1
        trainings_by_tasks[e].append((t,p,i+1))
    task_times = []
    for trainings in trainings_by_tasks:
        dp = [(None,[]) for i in range(101)]
        dp[0] = (0,[])
        for (t,p,training_index) in trainings:
            for i in range(100,-1,-1):
                time = dp[i][0]
                if time is None:
                    continue
                next = min(i + p,100)
                next_time = time + t
                if dp[next][0] is None or next_time < dp[next][0]:
                    dp[next] = (next_time, dp[i][1] + [training_index])
        if dp[100][0] is None:
            print(-1)
            return
        task_times.append((dp[100][0], dp[100][1]))
    curr_time = 0
    ans = 0
    for (i,(time,sequence)) in enumerate(task_times):
        curr_time += time
        deadline = a[i]
        ans += len(sequence)
        if curr_time > deadline:
            print(-1)
            return
    print(ans)
    for time,sequence in task_times:
        print(*sequence,end=" ")
    print()


for t in range(int(input())):
    solve()


Comments

Submit
0 Comments
More Questions

1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME